package cn.wolfcode.shop.cloud.service;

import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.key.KeyPrefix;
import cn.wolfcode.shop.cloud.msg.RedisCodeMsg;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;

//基于jedis 封装RedisService
public class RedisService {
    //注入jedis连接池
    @Autowired
    private JedisPool jedisPool;

    //set方法
    public <T> void set(KeyPrefix keyPrefix, String key, T data) {
        // relKey=前缀+key
        //写在（）里自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String relKey = keyPrefix.getPrefix() + key;
            //如果过期时间大于0，设置过期时间,否则直接设置就行了
            if (keyPrefix.getExpireSeconds() > 0) {
                jedis.setex(relKey, keyPrefix.getExpireSeconds(), JSON.toJSONString(data));
            } else {
                jedis.set(relKey, JSON.toJSONString(data));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);

        }

    }
    //get方法
    public <T> T get(KeyPrefix keyPrefix, String key, Class<T> clz) {
        // relKey=前缀+key
        //写在（）里自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String relKey = keyPrefix.getPrefix() + key;
            String redisValue = jedis.get(relKey);
            if (StringUtils.isEmpty(redisValue)){
                return null;
            }else {
               return JSON.parseObject(redisValue,clz);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }

    }
    //incr方法
    public Long incr(KeyPrefix keyPrefix, String key) {
        // relKey=前缀+key
        //写在（）里自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String relKey = keyPrefix.getPrefix() + key;
           return jedis.incr(relKey);

        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }

    }
    //decr方法
    public Long decr(KeyPrefix keyPrefix, String key) {
        // relKey=前缀+key
        //写在（）里自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String relKey = keyPrefix.getPrefix() + key;
            return jedis.decr(relKey);

        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }

    }
    //expire方法/设置过期时间
    public Long expire(KeyPrefix keyPrefix, String key) {
        // relKey=前缀+key
        //写在（）里自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String relKey = keyPrefix.getPrefix() + key;
            return jedis.expire(relKey,keyPrefix.getExpireSeconds());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }

    }
    //exists方法/设置过期时间
    public boolean exists(KeyPrefix keyPrefix, String key) {
        // relKey=前缀+key
        //写在（）里自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String relKey = keyPrefix.getPrefix() + key;
            return jedis.exists(relKey);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }

    }
    //hset方法
    public <T> void hSet(KeyPrefix keyPrefix, String key,String field, T data) {
        // relKey=前缀+key
        //写在（）里自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String relKey = keyPrefix.getPrefix() + key;
                jedis.hset(relKey,field, JSON.toJSONString(data));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }
    //hget方法
    public <T> T hGet(KeyPrefix keyPrefix, String key,String field ,Class<T> clz) {
        // relKey=前缀+key
        //写在（）里自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String relKey = keyPrefix.getPrefix() + key;
        return  JSON.parseObject(jedis.hget(relKey,field),clz);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }
    //hgetAll方法
    public <T> Map<String,T> hGetAll(KeyPrefix keyPrefix, String key, Class<T> clz) {
        // relKey=前缀+key
        //写在（）里自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String relKey = keyPrefix.getPrefix() + key;
            Map<String, String> stringStringMap = jedis.hgetAll(relKey);
            Map<String,T> ret=  new HashMap<>();
            for (Map.Entry<String, String> entry : stringStringMap.entrySet()) {
                ret.put(entry.getKey(),JSON.parseObject(entry.getValue(),clz));
            }
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }
}
